/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package easybooking.clients.gui;

import easybooking.data.Airline;
import easybooking.data.Airport;
import easybooking.data.Flight;
import easybooking.data.Plane;
import java.rmi.RemoteException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author tim
 */
public class FlightTableModel extends AbstractTableModel {

    private Vector<Flight> flights;
     private EBInterfaceClient ebic;
    private static final int ID_INDEX = 0;
    private static final int DATE_INDEX = 1;
    private static final int PLANE_INDEX = 2;
    private static final int START_INDEX = 3;
    private static final int STOP_INDEX = 4;
    private static final int OBJECT = 5;
    private String[] columnNames = {"ID", "Date", "Plane", "Start", "Stop"};


    public FlightTableModel(Vector<Flight> f, EBInterfaceClient ebifc) {
        this.ebic = ebifc;
        flights = f;
    }
/**
 * get the number of rows the table needs
 * @return the number of flight objects in the flights Vector
 */
    public int getRowCount() {
        return flights.size();
    }
/**
 * get the number of columns
 * @return the number of the column Names
 */
    public int getColumnCount() {
        return columnNames.length;

    }

    @Override
            /**
     * get the name of a column
     * @param col is the number of the column which name is wanted
     * @return the name of a column
     */
    public String getColumnName(int col) {
        return columnNames[col];
    }
/**
 * prepares the airline data for the table
 * @param rowIndex the row in which the data get insert
 * @param columnIndex the column in which the data get insert
 * @return the prepared object
 */
    public Object getValueAt(int rowIndex, int columnIndex) {
        try {
            Flight f = (Flight) flights.get(rowIndex);
            switch (columnIndex) {
                case ID_INDEX:
                    return f.getFlightId();
                case DATE_INDEX:
                   return f.getDateTime();
                case PLANE_INDEX:
                    Plane p = ebic.findPlaneByKey(f.getPlaneId());;
                    return p.getName();
                case START_INDEX:
                    Airport sa = ebic.findAirportByKey(f.getStartAirportId());
                    return sa.getName();
                case STOP_INDEX:
                    Airport da = ebic.findAirportByKey(f.getDestAirportId());
                    return da.getName();
                case OBJECT:
                    return f;
                default:
                    return new Object();
            }
        } catch (RemoteException ex) {
            Logger.getLogger(FlightTableModel.class.getName()).log(Level.SEVERE, null, ex);
            return new Object();
        }
    }
}

